package com.junzhiit.nengji.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.dao.SystemUserRoleDAO;
import com.junzhiit.nengji.model.SystemUserRole;

@Repository
public class SystemUserRoleDAOImpl extends AbstractBaseDAO<SystemUserRole> implements SystemUserRoleDAO {

	@Override
	public int getCountByRoleId(String roleId) {
		String sql = "select count(id) from `system_user_role` where role_id = ?";
		StatementParameter param = new StatementParameter();
		param.setString(roleId);
		return super.queryForInt(sql, param);
	}

	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	@Override
	public boolean deleteByRoleId(String roleId) {
		String sql = "delete from `system_user_role` where role_id = ?";
		StatementParameter param = new StatementParameter();
		param.setString(roleId);
		return super.updateForBoolean(sql, param);
	}

	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	@Override
	public boolean deleteSystemUserRole(String systemUserId) {
		StringBuffer sql = new StringBuffer();
		sql.append(" delete from `system_user_role`");
		sql.append(" where system_user_id = ?");

		StatementParameter param = new StatementParameter();
		param.setString(systemUserId);

		return super.updateForBoolean(sql.toString(), param);
	}

	@Override
	public List<SystemUserRole> getSystemUserRole(String systemUserId) {
		StringBuffer sql = new StringBuffer();
		sql.append("select * from `system_user_role`");
		sql.append(" where system_user_id = ?");
		sql.append(" and is_delete=?");

		StatementParameter param = new StatementParameter();
		param.setString(systemUserId);
		param.setInt(IsDelete.NO.toIntValue());

		return super.queryForList(sql.toString(), SystemUserRole.class, param);
	}
}
